/**
 * Created by Administrator on 2017/6/27.
 */
define(['swiper', 'jquery', "popups", "vue"], function (swiper, $, popups, Vue) {
    var sw = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });
    mui.init();
    var buy = [];
    var po_vue = new Vue({
        el: '#popover',
        data: {
            is_collect: is_collect
        },
        methods: {
            collect: function (store_id) {
                $.post(collect_url, {store_id: store_id},
                    function (result) {
                        if (result.code === 1) {
                            po_vue.is_collect = 1;
                            mui.toast("店铺已收藏");
                        } else {
                            mui.toast(result.msg);
                        }
                    });
            },
            del_collect: function (store_id) {
                $.post(del_collect_url, {store_id: store_id},
                    function (result) {
                        if (result.code === 1) {
                            po_vue.is_collect = 0;
                            mui.toast("店铺已取消收藏");
                        } else {
                            mui.toast(result.msg);
                        }
                    });
            }
        }
    });
    var v_price = 0;
    var vue = new Vue({
        el: '#buy_page',
        data: {},
        methods: {
            detail: function (id) {
                window.location.href = detail_url + "/id/" + id + "/total_price/" + total_price;
            },
            down_tap: function (price, id, v_flag) {
                //event.stopPropagation();
                var target = $(event.currentTarget);
                var n = target.siblings('.num');

                var num = n.val();
                num--;
                if(v_flag===1){
                    if(v_price>0){
                        total_price -= parseInt(v_price, 10);
                    }else {
                        total_price -= parseInt(price, 10);
                    }
                }else {
                    total_price -= parseInt(price, 10);
                }
                n.val(num);
                if (num < 1) {
                    n.hide();
                    target.hide();
                }
                document.getElementById("total_price").innerHTML = total_price.toFixed(2) + "元";
                var data = {
                    "type": "del",
                    'variant_id': 0,
                    "goods_id": id
                };
                $.post(add_cart_url, data,
                    function (result) {
                        if (result.code !== 1) {
                            mui.alert(result.msg, '系统提示', function () {
                                location.reload();
                            }, 'div');
                        }
                    });
            },
            up_tap: function (price, id, v_flag, goods_name) {
                var target = $(event.currentTarget);
                var n = target.siblings('.num');
                var s = target.siblings('.down');
                if (v_flag === 0) {
                    var num = n.val();
                    n.val(++num);
                    n.show();
                    s.show();
                    total_price += parseInt(price, 10);
                    document.getElementById("total_price").innerHTML = total_price.toFixed(2) + "元";
                    var data = {
                        "type": "add",
                        'variant_id': 0,
                        "goods_id": id
                    };
                    $.post(add_cart_url, data,
                        function (result) {
                            if (result.code !== 1) {
                                mui.alert(result.msg, '系统提示', function () {
                                    location.reload();
                                }, 'div');
                            }
                        });
                } else {
                    vue_popups.a_index = 0;
                    vue_popups.v_goods_id = id;
                    vue_popups.v_goods_name = goods_name;
                    vue_popups.n = n;
                    vue_popups.s = s;
                    $.post(variant_url, {"id": id},
                        function (result) {
                            if (result.code === 1) {
                                vue_popups.a_index = 0;
                                vue_popups.v_items = result.data;
                                vue_popups.v_price = vue_popups.v_items[0].price;
                                vue_popups.v_id = vue_popups.v_items[0].id;
                                popups.popups();
                            } else {
                                mui.alert(result.msg, '系统提示', function () {
                                    location.reload();
                                }, 'div');
                            }
                        });
                }
            },
            send_btn: function () {
                $.post(cart_url, {store_id: store_id},
                    function (result) {
                        if (result.code === 1) {
                            window.location.href = send_order_url + "/data/" + result.data;
                        } else {
                            //mui.alert("您没购买任何商品");
                            mui.alert("您没购买任何商品", '系统提示', function () {
                                location.reload();
                            }, 'div');
                        }
                    });
            }
        }
    });
    var vue_popups = new Vue({
        el: '#popups',
        data: {
            v_items: [],
            v_price: 0,
            v_id: 0,
            v_goods_id: 0,
            v_goods_name: "",
            n: null,
            s: null,
            a_index: 0
        },
        methods: {
            variant: function (v_id, v_price, a_index) {
                vue_popups.v_id = v_id;
                vue_popups.v_price = v_price;
                vue_popups.a_index = a_index;//选中的规格
            },
            save_variant: function () {
                var n = vue_popups.n;
                var s = vue_popups.s;
                if (n !== null && s !== null) {
                    var num = n.val();
                    n.val(++num);
                    n.show();
                    s.show();
                    total_price += parseInt(vue_popups.v_price, 10);
                    document.getElementById("total_price").innerHTML = total_price.toFixed(2) + "元";
                    var data = {
                        "type": "add",
                        'variant_id': vue_popups.v_id,
                        "goods_id": vue_popups.v_goods_id
                    };
                    $.post(add_cart_url, data,
                        function (result) {
                            if (result.code !== 1) {
                                mui.alert(result.msg, '系统提示', function () {
                                    location.reload();
                                }, 'div');
                            } else {
                                v_price = vue_popups.v_price;
                            }
                            popups.close(document.querySelector('.popups-module'));
                        });
                }
            }
        }
    })

});